<template>
	<view>
		<view class="goods-item" @click="handlerClick">
			<view class="goods-item-left">
				<radio :checked="goods.goods_state" color="#c00000" v-if="showRadio" @click="radioStateHandler"></radio>
				<image :src="goods.goods_small_logo || defaultPic"></image>
			</view>
			<view class="goods-item-right">
				<view class="goods-item-name">{{goods.goods_name}}</view>
				<view class="goods-item-box">
					<view class="goods-item-price">￥{{goods.goods_price | toFixed}}</view>
					<uni-number-box :min="1" class="goods-item-count" v-if="showCount" :value="goods.goods_count"
						@change="countHandlerChange">
					</uni-number-box>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {mapMutations} from 'vuex'
	export default {
		name: "my-goods",
		props: {
			goods: {
				type: Object,
				default: () => {}
			},
			showRadio: {
				type: Boolean,
				default: false
			},
			showCount: {
				type: Boolean,
				default: () => {}
			}
		},
		filters: {
			toFixed(price) {
				return Number(price).toFixed(2)
			}
		},
		data() {
			return {
				defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
			};
		},
		methods: {
			...mapMutations('my_cart',['updateGoodsCount']),
			countHandlerChange(value) {
				this.updateGoodsCount({
					goods_id: this.goods.goods_id,
					goods_count: value
				})
			},
			handlerClick() {
				this.$emit('click', this.goods.goods_id)
			},
			radioStateHandler() {
				this.$emit('radioStateChange', {
					goods_id: this.goods.goods_id,
					goods_state: !this.goods.goods_state
				})
			}
		}

	}
</script>

<style lang="scss">
	.goods-item {
		display: flex;
		border-bottom: 1px solid #f0f0f0;
		padding: 10px 5px;

		.goods-item-left {
			display: flex;
			justify-content: space-around;
			align-items: center;
			margin-right: 5px;

			image {
				width: 100px;
				height: 100px;
				display: block;
			}
		}

		.goods-item-right {
			display: flex;
			flex-direction: column;
			justify-content: space-between;

			.goods-item-name {
				font-size: 13px;
			}

			.goods-item-box {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.goods-item-price {
					font-size: 16px;
					color: #c00000;
				}
			}


		}
	}
</style>
